import com.sun.source.tree.Tree;

import java.util.*;

public class MyTreeNode {

    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }

    //二叉树的根节点
//    public TreeNode root;

    public TreeNode createTree() {
        TreeNode A = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        A.left = B;
        A.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;

        return A;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root) {
        if (root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }


    public static int count = 0;

    //获取树中节点的个数  遍历思路 O(N)
    public void size(TreeNode root) {
        if (root == null) {
            return;
        }
        count++;
        size(root.left);
        size(root.right);
    }

    //获取树中节点的个数  子问题思路 O(N)
    public int nodeSize(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return nodeSize(root.left) + nodeSize(root.right) + 1;
    }


    public static int leafCount = 0;
    //获取叶子节点的个数  遍历思路
    public void getLeafNodeCount(TreeNode root) {
        if (root == null) {
            return ;
        }
        if (root.left == null && root.right == null) {
            leafCount++;
        }
        getLeafNodeCount(root.left);
        getLeafNodeCount(root.right);

    }

    //获取叶子节点的个数  子问题思路
    public int getLeafNodeCount2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeCount2(root.left) + getLeafNodeCount2(root.right);

    }

    //第K层的节点数
    public int getKLevelNodeCount(TreeNode root,int k) {
        if (root == null) {
            return 0;
        }
        if (k == 1) {
            return 1;
        }
        return getKLevelNodeCount(root.left,k-1) + getKLevelNodeCount(root.right,k-1);
    }

    //获取⼆叉树的⾼度
    public int getHeight(TreeNode root){
        if (root == null) {
            return 0;
        }
        int leftH = getHeight(root.left);
        int rightH = getHeight(root.right);
        return Math.max(leftH,rightH) + 1;
    }

    public int getHeight1(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(getHeight1(root.left),getHeight1(root.right)) + 1;
    }


    //检测值为value的元素是否存在
    public TreeNode find(TreeNode root,char val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }

        TreeNode ret1 = find(root.left,val);
        if (root != null) {
            return ret1;
        }

        TreeNode ret2 = find(root.right,val);
        if (root != null) {
            return ret2;
        }
        return null;

    }

    public TreeNode find1(TreeNode root,char val) {
        if (root == null) {
            return null;
        }

        if (root.val == val) {
            return root;
        }

        TreeNode leftTree = find1(root.left,val);
        if (root != null) {
            return leftTree;
        }

        TreeNode rightTree = find1(root.right,val);
        if (root != null) {
            return rightTree;
        }
        return null;
    }

    //翻转二叉树
    public TreeNode invertTree(TreeNode root) {

        if (root == null) {
            return null;
        }

        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        invertTree(root.left);
        invertTree(root.right);

        return root;

    }

     //平衡二叉树
    //O(N^2)
     public boolean isBalanced(TreeNode root) {
         if (root == null) {
             return true;
         }
         int leftH = getHeight(root.left);
         int rightH = getHeight(root.right);
         return Math.abs(leftH-rightH) <= 1 && isBalanced(root.left) && isBalanced(root.right);
     }

    //O(N)
    public boolean isBalanced2(TreeNode root) {
        if (root == null) {
            return true;
        }
        return getHeight2(root) >= 0;
    }

    public int getHeight2(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftH = getHeight(root.left);

        int rightH = getHeight(root.right);

        if (leftH >= 0 && rightH >= 0 && Math.abs(leftH - rightH) <= 1) {
            return leftH > rightH ? leftH + 1 : rightH + 1;
        } else {
            return -1;
        }
    }


    //二叉树遍历
    public static int i = 0;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextLine()) { // 注意 while 处理多个 case
            String str = in.nextLine();
            TreeNode root = createTree(str);
            inOrder1(root);
        }
    }

    public static TreeNode createTree(String str) {
        char ch = str.charAt(i);
        TreeNode root = null;
        if (ch != '#') {
            root = new TreeNode(ch);
            i++;
            root.left = createTree(str);
            root.right = createTree(str);
        }else {
            i++;
        }
        return root;
    }

    public static void inOrder1(TreeNode root) {
        if (root == null) {
            return;
        }
        inOrder1(root.left);
        System.out.print(root.val + " ");
        inOrder1(root.right);
    }

    //层序遍历
    public void levelOrder(TreeNode root) {
        if (root == null) {
            return;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            TreeNode tmp = queue.poll();
            System.out.print(tmp.val + " ");
            if (tmp.left != null) {
                queue.offer(tmp.left);
            }
            if (tmp.right != null) {
                queue.offer(tmp.right);
            }
        }

    }

    public List<List<Character>> levelOrderBottom1(TreeNode root) {
        List<List<Character>> list = new ArrayList<>();

        if (root == null) {
            return list;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            List<Character> curRow = new ArrayList<>();
            int size = queue.size();
            while (size != 0) {
                TreeNode tmp = queue.poll();
//                    System.out.print(tmp.val + " ");
                curRow.add(tmp.val);
                if (tmp.left != null) {
                    queue.offer(tmp.left);
                }
                if (tmp.right != null) {
                    queue.offer(tmp.right);
                }
                size--;
            }
            list.add(curRow);
        }
        return list;

    }

    //二叉树的最近公共祖先
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root == null){
            return null;
        }
        if(root == p || root == q){
            return root;
        }
        TreeNode leftTree = lowestCommonAncestor(root.left,p,q);
        TreeNode rightTree = lowestCommonAncestor(root.right,p,q);
        if(leftTree != null && rightTree != null){
            return root;
        }else if(leftTree != null){
            return leftTree;
        }else{
            return rightTree;
        }
    }

    //二叉树的层序遍历
    public List<List<Character>> levelOrder1(TreeNode root) {
        List<List<Character>> ret = new ArrayList<>();

        if (root == null) {
            return ret;
        }

        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            List<Character> curRow = new ArrayList<>();
            int size = queue.size();
            while (size != 0) {
                TreeNode tmp = queue.poll();
                curRow.add(tmp.val);
                if (tmp.left != null) {
                    queue.offer(tmp.left);
                }
                if (tmp.right != null) {
                    queue.offer(tmp.right);
                }
                size--;
            }
            ret.add(curRow);
        }
        return ret;
    }


    //前序中序还原二叉树
    public int preIndex = 0;

    public TreeNode buildTree(char[] preorder, char[] inorder) {
        return buildTreeChild(preorder,inorder,0,inorder.length - 1);
    }

    public int findVal(char[] inorder, int inbegin, int inend, int key) {
        for (int i = inbegin; i<=inend; i++) {
            if (inorder[i] == key) {
                return i;
            }
        }
        return -1;
    }

    public TreeNode buildTreeChild(char[] preorder, char[] inorder, int inbegin, int inend) {
        if (inbegin > inend) {
            return null;
        }

        TreeNode root = new TreeNode(preorder[preIndex]);

        //在中序遍历 找到根的位置
        int rootIndex = findVal(inorder,inbegin,inend,preorder[preIndex]);
        preIndex++;

        //左树 右树
        root.left = buildTreeChild(preorder,inorder,inbegin,rootIndex - 1);
        root.right = buildTreeChild(preorder,inorder,rootIndex + 1,inend);

        return root;
    }


    //二叉树的前序遍历
     List<Character> list1 = new LinkedList<>();
     public List<Character> preorderTraversal(TreeNode root) {
         if(root == null) {
             return list1;
         }
         list1.add(root.val);
         preorderTraversal(root.left);
         preorderTraversal(root.right);
         return list1;
     }

     public List<Character> preorderTraversal1(TreeNode root) {
         List<Character> list = new ArrayList<>();
         if(root == null) {
             return list;
         }
         list.add(root.val);
         List<Character> listLeft = preorderTraversal(root.left);
         list.addAll(listLeft);
         List<Character> listRight = preorderTraversal(root.right);
         list.addAll(listRight);
         return list;
     }

    //非递归实现
    public List<Character> preorderTraversal2(TreeNode root) {
        List<Character> list = new ArrayList<>();

        if(root == null) {
            return list;
        }

        TreeNode cur = root;
        TreeNode top = null;

        Stack<TreeNode> stack = new Stack<>();

        while(cur != null || !stack.isEmpty()){
            while(cur != null) {
                stack.push(cur);
                list.add(cur.val);
                cur = cur.left;
            }
            top = stack.pop();
            cur = top.right;
        }

        return list;
    }


    //二叉树的中序遍历
     List<Character> list2 = new LinkedList<>();
     public List<Character> inorderTraversal(TreeNode root) {
         if(root == null) {
             return list2;
         }
         inorderTraversal(root.left);
         list2.add(root.val);
         inorderTraversal(root.right);
         return list2;
     }

     public List<Character> inorderTraversal1(TreeNode root) {
         List<Character> list = new ArrayList<>();
         if(root == null) {
             return list;
         }
         List<Character> listLeft = inorderTraversal(root.left);
         list.addAll(listLeft);
         list.add(root.val);
         List<Character> listRight = inorderTraversal(root.right);
         list.addAll(listRight);
         return list;
     }

    //非递归实现
    public List<Character> inorderTraversal2(TreeNode root) {
        List<Character> list = new ArrayList<>();

        if(root == null) {
            return list;
        }

        TreeNode cur = root;
        TreeNode top = null;

        Stack<TreeNode> stack = new Stack<>();

        while(cur != null || !stack.isEmpty()){
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            top = stack.pop();
            list.add(top.val);
            cur = top.right;
        }

        return list;
    }

    //二叉树的后序遍历
     List<Character> list3 = new LinkedList<>();
     public List<Character> postorderTraversal(TreeNode root) {
         if(root == null) {
             return list3;
         }
         postorderTraversal(root.left);
         postorderTraversal(root.right);
         list3.add(root.val);
         return list3;
     }

     public List<Character> postorderTraversal1(TreeNode root) {
         List<Character> list = new ArrayList<>();
         if(root == null) {
             return list;
         }
         List<Character> listLeft = postorderTraversal(root.left);
         list.addAll(listLeft);
         List<Character> listRight = postorderTraversal(root.right);
         list.addAll(listRight);
         list.add(root.val);
         return list;
     }

    public List<Character> postorderTraversal2(TreeNode root) {
        List<Character> ret = new ArrayList<>();
        // 空树直接返回
        if(null == root){
            return ret;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        TreeNode prev = null;
        while(cur != null || !stack.isEmpty()) {
            while(cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            top = stack.peek();
            if (top.right == null || top.right == prev) {
                stack.pop();
                ret.add(top.val);
                prev = top;
            }else {
                cur = top.right;
            }
        }
        return ret;
    }

}
